<template>
	<div class="itemRowEdit-popup">
		<w-header :title="currentItemData.goodsDTO.name" :leftOptions="leftOptions" @on-click-back="onCloseClick"></w-header>
		<div ref="rowEditContent" class="itemRowEdit-content">
			<van-cell-group>
				<van-cell title="单位" :value="unitName" value-class="info-item-value"/>
				<van-cell
					v-show="$fxAuth('shopCheckBill.seeAmount')"
					title="账存数量" :value="currentItemData.accountAmount"
					value-class="info-item-value"
				/>
				<van-cell
					v-show="$fxAuth('shopCheckBill.seeMoney')"
					title="账存金额"
					:value="currentItemData.accountMoney"
					value-class="info-item-value"/>
				<van-cell
					v-show="$fxAuth('shopCheckBill.seeDiffAmount')"
					title="差异数量"
					:value="showByChecked(currentItemData.diffAmount)"
					value-class="info-item-value"/>
				<van-cell v-show="$fxAuth('shopCheckBill.seeCheckOutMoney')" title="盈亏金额" :value="showByChecked(currentItemData.checkInOutMoney)" value-class="info-item-value"/>
				<van-cell title="品项编号" :value="currentItemData.goodsDTO.code" value-class="info-item-value"/>
				<van-cell v-show="!hasSumBatch" title="批次号" :value="currentItemData.batchCode" value-class="info-item-value"/>
				<van-switch-cell
					v-if="$fxSysAuth('shopCheckBill.viewSync')"
					:value="formatType(currentItemData.synStock)"
					:disabled="isDisabled" class="info-item-value"
					title="同步库存"
					@input="onSynStockInput"
				/>
				<van-pro-field
					ref="memo"
					v-model="currentItemData.memo"
					:disabled="isDisabled"
					:placeholder="setPlaceholder('请输入备注')"
					label="备注"
					input-align="right"
					@focus="onFocus('memo')"
				/>
				<van-cell v-show="sysShowDZCData" title="净重量" :value="toFixedNumberSize(currentItemData.netWeight)" value-class="info-item-value"></van-cell>
				<van-cell v-show="sysShowDZCData" title="皮重量" :value="toFixedNumberSize(currentItemData.skinWeight)" value-class="info-item-value"></van-cell>
				<van-cell v-show="sysShowDZCData" title="毛重量" :value="toFixedNumberSize(currentItemData.grossWeight)" value-class="info-item-value"></van-cell>
				<van-cell v-show="sysShowDZCData" title="称重单位" :value="currentItemData.weightUnit" value-class="info-item-value"></van-cell>
				<van-cell v-show="sysShowDZCData" title="称重状态" :value="currentItemData.isWeightSign ? '已称重' : '未称重'" value-class="info-item-value"></van-cell>
				<van-cell v-show="sysShowDZCData" title="图片" :border="false" value-class="info-item-value"></van-cell>
				<van-cell v-show="sysShowDZCData && currentItemData.images.length" class="info-cell-img" value-class="info-item-img">
					<img :src="comptedItemImgSrc(currentItemData.images)">
				</van-cell>
			</van-cell-group>
		</div>
		<div class="itemRowEdit-bottom">
			<van-button
				v-if="!isDisabled"
				class="confirm-btn fx-no-radius"
				type="primary"
				@click="onConfirmClick"
			>保存</van-button>
			<van-button
				v-else
				class="confirm-btn fx-no-radius"
				type="primary"
				@click="onCloseClick"
			>确定</van-button>
		</div>
	</div>
</template>

<script>
import { mapState } from 'vuex'
export default {
	name: 'itemRowEdit',
	props: {
		visibleFlag: Boolean,
		itemData: {
			type: Object,
			default: () => {
				return {}
			}
		},
		isCheckedBill: Boolean,
		isDisabled: Boolean,
		hasSumBatch: Boolean
	},
	data () {
		return {
			leftOptions: {
				showBackBtn: true,
				preventGoBack: true
			},
			currentItemData: {}
		}
	},
	computed: {
		...mapState({
			sysShowDZCData: state => state.systemParams.sysShowDZCData,
			sysPointSize: 'sysPointSize'
		}),
		unitName () {
			return this.currentItemData.planUnitDTO ? this.currentItemData.planUnitDTO.name : this.currentItemData.checkUnitDTO.name
		},
		showByChecked (data) {
			return (data) => {
				if (this.isCheckedBill) {
					return data
				} else {
					return ''
				}
			}
		},
		setPlaceholder (val) {
			return (val) => {
				return this.isDisabled ? '' : val
			}
		},
		comptedItemImgSrc (imageUrls) {
			return (imageUrls) => {
				if (!imageUrls) {
					return ''
				}
				return imageUrls.length === 0 ? '' : imageUrls[0].imagUrl
			}
		},
		formatType (date) {
			return (date) => {
				if (date === '1') {
					return true
				} else {
					return false
				}
			}
		}
	},
	watch: {
		visibleFlag: {
			handler (val) {
				if (val) {
					this.currentItemData = JSON.parse(JSON.stringify(this.itemData))
				}
			},
			immediate: true
		}
	},
	methods: {
		toFixedNumberSize (data) {
			return parseFloat(this.$fxUtils.toFixedSHW(data, this.sysPointSize))
		},
		onCloseClick () {
			this.$emit('update:visibleFlag', false)
		},
		onConfirmClick () {
			if (this.currentItemData.id) {
				this.currentItemData.changeType = 'edit'
			}
			this.$emit('on-Confirm', this.currentItemData)
			this.$emit('update:visibleFlag', false)
		},
		onFocus (ref) {
			this.onScrollTop(ref)
		},
		onScrollTop (scrollRef, distance = 20) {
			let detailsRowRef = this.$refs[scrollRef]
			if (!detailsRowRef || detailsRowRef.length === 0) {
				return false
			}
			let offsetTop = detailsRowRef.$el.offsetTop
			let $el = this.$refs['rowEditContent']
			this.$fxUtils.scrollTopW({ offsetTop, $el, distance })
		},
		onSynStockInput (checked) {
			if (checked) {
				this.currentItemData.synStock = '1'
			} else {
				this.currentItemData.synStock = '0'
			}
		}
	}
}
</script>
<style lang="stylus" scoped>
@import '~$assets/stylus/varsty.styl'
.itemRowEdit-popup {
	height:100%
	width: 100%
	text-align:left
	display: flex
	flex-direction: column
	.itemRowEdit-content {
		flex:1
		overflow:scroll
		.info-item-value {
			text-overflow: ellipsis;
			white-space: nowrap;
		}
		.info-cell-img {
			padding: 0
		}
		.info-item-img {
			padding:0 10px 15px 50px
			box-sizing:border-box
			&>img {
				width:100%
				height:100%
			}
		}
	}
	.itemRowEdit-bottom {
		display: flex
		flex-direction: row
		width:100%
		.confirm-btn {
			flex:1
		}
	}
}
</style>
